import {
  requestTips,
  askDialog
} from '@/common/js/utils.js';
import {
  accAdd,
  accMul,
  accDiv,
  monthDayDiff
} from '@/common/js/utils.js';

let contractCost = {
  props:{
    list: {
      type: Array,
      default: () => {
        return [];
      },
    },
  },

  watch: {
    list:{
      deep:true,
      immediate:true,
      handler(val){
        this.tableList = val || []
      }
    }
  },

  data() {
    return {

    }
  },

  methods: {
    //新增
    addItem() {
      this.tableList.push(JSON.parse(JSON.stringify(this.formObj)));
    },

    //删除
    deleteItem(item, index) {
      askDialog('确认删除？', async () => {
        this.tableList.splice(index, 1);
      });
    },

    //赋值assetName asssetCode
    setAssetTxt(row) {
      this.selfAssetsList.forEach(item => {
        if (row.shopKey == item.assetKey) {
          row.shopName = item.assetName;
          row.shopCode = item.assetCode;
        }
      });
    },

    //校验时间
    validateDate(row) {
      if (!row.rentStartDate) {
        row.rentEndDate = '';
        this.$message.warning('请输入开始时间!');
        return false;
      }
      let timestamp = row.rentEndDate - row.rentStartDate;
      if (row.rentEndDate && timestamp < 0) {
        row.rentEndDate = '';
        this.$message.warning('结束时间应大于开始时间');
        return false;
      }
      return true;
    },

    //保存
    saveFn() {
      let res = {[this.pageName]:this.tableList};
      return res;
    }
  },
}

class Calculate {
  //计算固定金额  固定金额(元/天)
  static setFixPriceByDay(unitPrice, unit, area) {
    let num = 0;
    if (unit == 'rent_calculate_day_unit' || unit == 'device_calculate_day_unit') {
      num = accMul(unitPrice, area); //固定金额(元/天) = 日均单价*面积
    } else if (unit == 'rent_calculate_month_unit' || unit == 'device_calculate_month_unit') {
      let num1 = accMul(unitPrice, area);
      let num2 = accMul(num1, 12);
      num = accDiv(num2, 365); //固定金额(元/天) = 月均单价*12*面积/365
    }
    return num || 0;
  }

  //计算单价  固定金额(元/天)
  static setUnitPriceByDay(fixPrice, unit, area) {
    let num = 0;
    if (unit == 'rent_calculate_day_unit' || unit == 'device_calculate_day_unit') {
      num = accDiv(fixPrice, area); //日均单价 = 固定金额(元/天)/面积
    } else if (unit == 'rent_calculate_month_unit' || unit == 'device_calculate_month_unit') {
      let num1 = accMul(fixPrice, 365);
      let num2 = accDiv(num1, 12);
      num = accDiv(num2, area); //月均单价 = 固定金额(元/天)*365/12/面积
    }
    return num || 0;
  }

  //计算固定金额  固定金额(元/月)
  static setFixPriceByMonth(unitPrice, unit, area) {
    let num = 0;
    if (unit == 'rent_calculate_day_unit' || unit == 'device_calculate_day_unit') {
      let num1 = accMul(unitPrice, area);
      let num2 = accMul(num1, 365);
      num = accDiv(num2, 12); //固定金额(元/月) = 日均单价*面积*365/12
    } else if (unit == 'rent_calculate_month_unit' || unit == 'device_calculate_month_unit') {
      num = accMul(unitPrice, area); //固定金额(元/月) = 月均单价*面积
    }
    return num || 0;
  }

  //计算单价  固定金额(元/月)
  static setUnitPriceByMonth(fixPrice, unit, area) {
    let num = 0;
    if (unit == 'rent_calculate_day_unit' || unit == 'device_calculate_day_unit') {
      let num1 = accMul(fixPrice, 12);
      let num2 = accDiv(num1, 365);
      num = accDiv(num2, area); //日均单价 = 固定金额(元/月)*12/365/面积
    } else if (unit == 'rent_calculate_month_unit' || unit == 'device_calculate_month_unit') {
      num = accDiv(fixPrice, area); //月均单价 = 固定金额(元/月)/面积
    }
    return num || 0;
  }

  //计算固定金额  固定金额(元/年)
  static setFixPriceByYear(unitPrice, unit, area) {
    let num = 0;
    if (unit == 'rent_calculate_day_unit' || unit == 'device_calculate_day_unit') {
      let num1 = accMul(unitPrice, area);
      num = accMul(num1, 365); //固定金额(元/年) = 日均单价*面积*365
    } else if (unit == 'rent_calculate_month_unit' || unit == 'device_calculate_month_unit') {
      let num1 = accMul(unitPrice, area);
      num = accMul(num1, 12); //固定金额(元/年) = 月均单价*面积*365
    }
    return num || 0;
  }

  //计算单价  固定金额(元/年)
  static setUnitPriceByYear(fixPrice, unit, area) {
    let num = 0;
    if (unit == 'rent_calculate_day_unit' || unit == 'device_calculate_day_unit') {
      let num1 = accDiv(fixPrice, 365);
      num = accDiv(num1, area); //日均单价 = 固定金额(元/年)/365/面积
    } else if (unit == 'rent_calculate_month_unit' || unit == 'device_calculate_month_unit') {
      let num1 = accDiv(fixPrice, 12);
      num = accDiv(num1, area); //月均单价 = 固定金额(元/年)/12/面积
    }
    return num || 0;
  }

  //计算固定金额  固定金额(元)
  static setFixPriceAll(unitPrice, unit, area, beginTime, endTime) {
    let num = 0;
    let judgeDayObj = monthDayDiff(beginTime, endTime);
    let dayMoney = 0;
    let yearMoney = 0;
    let monthMoney = 0;
    if (unit == 'rent_calculate_day_unit' || unit == 'device_calculate_day_unit') {
      dayMoney = accMul(unitPrice, area); //日均：日均单价 * 面积
      yearMoney = accMul(dayMoney, 365); //年均：日均单价 * 面积 * 365
      monthMoney = accDiv(yearMoney, 12); //月均：日均单价 * 面积 * 365 / 12
    } else if (unit == 'rent_calculate_month_unit' || unit == 'device_calculate_month_unit') {
      monthMoney = accMul(unitPrice, area); //月均：月均单价 * 面积
      yearMoney = accMul(monthMoney, 12); //月均：月均单价 * 12
      dayMoney = accDiv(yearMoney, 365); //日均：月均单价 * 12 / 365
    }
    num = accMul(monthMoney, judgeDayObj.year * 12) + accMul(monthMoney, judgeDayObj.month) + accMul(dayMoney, judgeDayObj.day)
    return num > 0 ? num : 0;
  }

  //计算单价  固定金额(元)
  static setUnitPriceAll(fixPrice, unit, area, beginTime, endTime) {
    let num = 0;
    let judgeDayObj = monthDayDiff(beginTime, endTime);
    let allMultiplier = 0;
    if (unit == 'rent_calculate_day_unit' || unit == 'device_calculate_day_unit') {
      // let num1 = accMul(area, 365);
      // let yearMultiplier = accMul(num1, judgeDayObj.year);
      // let num2 = accMul(area, 365);
      // let num3 = accDiv(num2, 12);
      // let monthMultiplier = accMul(num3, judgeDayObj.month);
      // let dayMultiplier = accMul(area, judgeDayObj.day);
      // let num4 = accAdd(yearMultiplier, monthMultiplier)
      // allMultiplier = accAdd(num4, dayMultiplier)
      allMultiplier = area * (judgeDayObj.day + 365/12*judgeDayObj.month + 365*judgeDayObj.year);
    } else if (unit == 'rent_calculate_month_unit' || unit == 'device_calculate_month_unit') {
      // let yearMultiplier = accMul(area, judgeDayObj.year * 12);
      // let monthMultiplier = accMul(area, judgeDayObj.month);
      // let num = accMul(area, judgeDayObj.day);
      // let dayMultiplier = accMul(num, 12 / 365);
      // let num1 = accAdd(yearMultiplier, monthMultiplier);
      // allMultiplier = accAdd(num1, dayMultiplier)
      allMultiplier = area * (judgeDayObj.day * 12 / 365 + judgeDayObj.month + judgeDayObj.year * 12);
    }
    num = accDiv(fixPrice, allMultiplier);
    return num > 0 ? num : 0;
  }
}

export {
  contractCost,
  Calculate
}
